package com.romotc.qqrobot.webqq.qqInterfaceCall;

import java.util.ArrayList;

import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.SysConstant;
import com.romotc.qqrobot.webqq.WebQQConstant;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.DateProcessor;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.Ptlogin2_Check_Processor;
import com.romotc.qqrobot.webqq.util.QQPswdUtil;
import com.romotc.scache.RequestSpy;
import com.romotc.scache.httpreq;
/**
 * 一次登录接口
 * @author romotc
 *
 */
public class Ptlogin2_Login_InterCall extends QQInterfaceCall {
	private static Ptlogin2_Login_InterCall instance;
	public static synchronized Ptlogin2_Login_InterCall getInstance() {
		if(instance == null)
			instance = new Ptlogin2_Login_InterCall();
		
		return instance;
	}

	/**
	 * QQ号密码
	 */
	public static final String Key_Login_Param_Pswd = D_Login2_InterCall.Key_Login_Param_Pswd;
	public static final String Key_Login_Param_QQ = D_Login2_InterCall.Key_Login_Param_QQ;
	public static final String Key_Login_Param_Status = D_Login2_InterCall.Key_Login_Param_Status;

	@Override
	public JSONObject preCall(QQLogedin loginQQ, JSONObject reqDataObj)
			throws Exception {
		Ptlogin2_Check_InterCall ptcheck = new Ptlogin2_Check_InterCall();
		return ptcheck.call(loginQQ, reqDataObj, Ptlogin2_Check_Processor.getInstance());
	}

	@Override
	public ArrayList<String> myReqHeaders(int reqBodyLen, QQLogedin loginQQ,JSONObject reqDataObj) {
//		Accept:*/*
//		Accept-Charset:UTF-8,*;q=0.5
//		Accept-Encoding:gzip,deflate,sdch
//		Accept-Language:zh-CN,zh;q=0.8
//		Connection:keep-alive
//		Cookie:verifysession=h006bc317b1abbd52c47d4a9333e7a66931f30fbe3f29b0397bbd4a77ecc905f787658be9d503e12b202195a15b6f02c811; ptui_qstatus=2; pt2gguin=o0097190865; uin=o0097190865; skey=@6F2iXMXbN; ptisp=cnc; ptuserinfo=526f6d6f7463; show_id=; ptcz=542d916d41e2c5ae81779a9a4762783b6893ac7884b48450d48cd486b8b9d754; pgv_pvid=4802251185; pgv_info=pgvReferrer=&ssid=s7724504450; uikey=47de3be33a26a0ed5e328f1490b0183c6c73c7e9011bb18f097638b9f64cfff3; chkuin=97190865; ptvfsession=5690f7092e273a7a96bbe903a4a97a5b1cf315a6194c62fce8ff3152d5b90ccfde8e9e62b58cb23d17ed3992f995e488; confirmuin=97190865
//		Host:ptlogin2.qq.com
//		Referer:http://ui.ptlogin2.qq.com/cgi-bin/login?target=self&style=5&mibao_css=m_webqq&appid=1003903&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fweb.qq.com%2Floginproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=20120320001
//		User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.83 Safari/535.11
		ArrayList<String> header = new ArrayList<String>();
//		header.add("Accept:*/*");
		header.add("Accept-Charset:UTF-8,*;q=0.5");
		header.add("Accept-Encoding:gzip,deflate,sdch");
		header.add("Accept-Language:zh-CN,zh;q=0.8");
		header.add("Connection:keep-alive");
		header.add("Cookie:" + loginQQ.getCookieString());
		header.add("Host:ptlogin2.qq.com");
		header.add("Referer:http://ui.ptlogin2.qq.com/cgi-bin/login?target=self&style=5&mibao_css=m_webqq&appid=" + WebQQConstant.APPID + 
				"&enable_qlogin=0&no_verifyimg=1&s_url=http%3A%2F%2Fweb.qq.com%2Floginproxy.html&f_url=loginerroralert&strong_login=1&login_state=10&t=" + WebQQConstant.T);
		header.add("User-Agent:" + SysConstant.User_Agent);
		return header;
	}

	@Override
	public String buildReqBody(JSONObject reqDataObj, QQLogedin loginQQ)
			throws Exception {
		return null;
	}

	@Override
	public int getLogLevel() {
		return 1001;
	}

	@Override
	public int getReq_method() {
		return httpreq.REQUEST_GET;
	}

	@Override
	public String getReqUrl(JSONObject reqDataObj, QQLogedin loginQQ) throws Exception {
		int webqqType =  WebQQConstant.getWebQQTypeFromLoginStatus(reqDataObj.optString(Key_Login_Param_Status, WebQQConstant.Login_Param_Status_Default));
		return WebQQConstant.URL_PTLOGIN2_LOGIN +
				"?u=" + reqDataObj.getString(Key_Login_Param_QQ) +
				//加密后的密码
				"&p=" + QQPswdUtil.mdP(reqDataObj.getString(Key_Login_Param_Pswd), reqDataObj.getString(Ptlogin2_Check_Processor.Key_VCode)) +
				//验证码
				"&verifycode=" + reqDataObj.getString(Ptlogin2_Check_Processor.Key_VCode) +
				"&webqq_type=" + webqqType + 
				"&remember_uin=1&login2qq=1&aid=" + WebQQConstant.APPID + 
				"&u1=http%3A%2F%2Fweb.qq.com%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D" + webqqType + "&h=1&ptredirect=0&ptlang=2052&from_ui=1&pttype=1&dumy=&fp=loginerroralert" +
				"&action=1-0-16312" + //RomUnDo暂时不用这个参数 来源于comm.js中action值，为记录的页面操作 action=" + pt.action.join("-") + "-" + (new Date() - g_begTime)
				"&mibao_css=m_webqq";
		
		//http://ptlogin2.qq.com/login?u=97190865&p=16C84367EFD63547D2EE9D408BAB176F&verifycode=!IB4&webqq_type=10
		//&remember_uin=1&login2qq=1&aid=1003903&u1=http%3A%2F%2Fweb.qq.com%2Floginproxy.html%3Flogin2qq%3D1%26webqq_type%3D10&h=1&ptredirect=0&ptlang=2052&from_ui=1&pttype=1&dumy=&fp=loginerroralert&action=1-13-5789&mibao_css=m_webqq
		
	}

	@Override
	public Class getResClassType() {
		return null;
	}
	
	public static void main(String[] args) throws Exception{
		Ptlogin2_Login_InterCall call = Ptlogin2_Login_InterCall.getInstance();
		QQLogedin curQqLogedIn = new QQLogedin();
		JSONObject reqDataObj = new JSONObject();
		reqDataObj.put(Key_Login_Param_QQ, "97190865");
		reqDataObj.put(Ptlogin2_Login_InterCall.Key_Login_Param_Pswd, "xxxx");
		
		call.call(curQqLogedIn, reqDataObj, new DateProcessor() {
			
			@Override
			public JSONObject processResponse(QQLogedin loginQQ, RequestSpy rqSpy,
					JSONObject reqDataObj) throws Exception {
				//ptuiCB('0','0','http://web.qq.com/loginproxy.html?login2qq=1&webqq_type=30','0','登录成功！', 'Romotc');
				//ptuiCB('3','0','','0','您输入的帐号或者密码不正确，请重新输入。', '97190867');
				String resbody = rqSpy.getResBody();
				System.out.println(resbody);
				return reqDataObj;
			}
		});
	}
}
